// @ts-ignore
import { ElNotification } from 'element-plus'

export default class Download {
  private static notify: any = ElNotification

  public static async downLoadFile(url: string, name: string) {
    const remote = !url.includes('blob')
    if (remote) {
      try {
        const blob = await ((await fetch(url)).blob())
        url = URL.createObjectURL(blob)
      } catch (e) {
        this.notify.warning({
          title: 'Prompt information',
          message: '下载异常，请重试',
          position: 'bottom-right'
        })
        return false
      }
    }
    const downElement = document.createElement('a')
    downElement.style.display = 'none'
    downElement.download = name
    downElement.href = url
    document.body.appendChild(downElement)
    downElement.click()
    document.body.removeChild(downElement)
    if (remote) {
      URL.revokeObjectURL(url)
    }
  }
}
